public class Solution1779 {
    public int nearestValidPoint(int x, int y, int[][] points) {
        int ans=-1,min=Integer.MAX_VALUE;
        for (int i=0;i<points.length;i++) {
            int[] point=points[i];
            if (point[0]==x || point[1]==y){
                int dis=Math.abs(x-point[0])+Math.abs(y-point[1]);
                if (dis<min){
                    min=dis;
                    ans=i;
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1779().nearestValidPoint(3,4,new int[][]{{1,2},{3,1},{2,4},{2,3},{4,4}}));
    }
}
